#include "http_file_download.h"

#include <iostream>
using namespace std;
using namespace bestv;

/*
	Some paths

	C:\Users\robert\Documents\Download_Manager\nlsclienttest\nlsclient\src\core\poco_libs\Poco\lib;
	C:\Users\robert\Documents\Download_Manager\nlsclienttest\nlsclient\src\core\poco_libs\lib;
	C:\Users\robert\Documents\Download_Manager\nlsclienttest\nlsclient\src\core\poco_libs\bin

*/


void autopointerTest()
{
	bestv::BtvString URL;
	bestv::BtvString fileName;
	//bestv::HttpFileDownload::statusT status;
	
	URL = "http://10.18.4.113/9MB.ts";

	fileName = "httpFileDownloadTest1.ts"; 

	bestv::BtvUInt64 startIndex(0, 0);

	std::auto_ptr<bestv::HttpFileDownload> httpFileDownload(
		new bestv::HttpFileDownload(URL, 
		fileName, 
		BandwidthManager::HIGH_PRIORITY, 
		startIndex));	

}

void main(){
	double bitrate;
	long num = 1L;
	
	btvClockT startTime;
	btvClockT stopTime;
	bestv::BtvUInt64 downloadedBytes;
	bestv::BtvString URL;
	bestv::BtvString fileName;
	bestv::HttpFileDownload::statusT status;

	/**********************************
			


				AUTO PTR



	***********************************/
	if(0){

		autopointerTest();
		printf("Presss Return To Continue\n");
		getchar();
	}

	if(1)
	{
		/* test download into a file */
		//bestv::BtvString URL("http://www.coolhunting.com/images/funky2.jpg"); 
		//URL = "http://127.0.0.1:8080/48MB.ts";
		URL = "http://127.0.0.1:8080/9MB.ts";
		//URL = "http://10.18.4.113/48MB.ts";
		//URL = "http://10.18.4.113/9MB.ts";
			
		fileName = "httpFileDownloadTest1.ts"; 

		bestv::BtvUInt64 startIndex(0, 0);

		startTime =  btvapiTimeNow();

		bestv::HttpFileDownload httpFileDownload(URL, 
			fileName, 
			BandwidthManager::HIGH_PRIORITY, 
			startIndex);

				
		if(httpFileDownload.start() != BTV_NO_ERROR) {
			printf("Error in start()\n");
			printf("Press Return To Finish\n");
			getchar();
			return ;
		}

		printf("Downloading %s -> %s\n", URL.data(), fileName.data());

		do{
			status = httpFileDownload.downloadDataChunk();	
		}
		while(status == HttpFileDownload::DOWNLOAD_NOT_FINISHED);

		
		btvClockT stopTime =  btvapiTimeNow();

		status = httpFileDownload.getStatus();
		
		if(status != HttpFileDownload::DOWNLOAD_FINISHED){
			printf("Error in download. Code %d\n", (int)status);
			printf("Press Return To Finish\n");
			getchar();
			return;
		}

		downloadedBytes = httpFileDownload.getNumberOfDownloadedBytes();

		printf("Downloaded bytes %d KB \n", downloadedBytes.getL() / 1024);
		httpFileDownload.stop();

		
		btvClockT ellapsedTime = btvapiTimeMinus(startTime, stopTime);

		/*
			Calculation valid only
			for files smaller than 2 GG.
			Because I take only the lower
			32 bits of downloadedBytes.
			Good enough for the tests.
		*/
	
		unsigned bitrate = 0;
		
		if(ellapsedTime){
			bitrate = (unsigned)(( 8. * downloadedBytes.getL() * btvapiClocksPerSec()) / ellapsedTime);
		}
		cout << "Average bitrate : " << bitrate/1000 << " kbit/s" << endl;

		printf("End of first test\n");
		printf("COMPARE THE ORIGIN FILE AND THE DOWNLOADED FILE BEFORE CONTINUING WITH THE NEXT TEST\n");
		printf("Press Return to Continue with the next test.\n");
		getchar();

	}

	if(0){
		printf("Test: Download File to Memory\n");
		downloadedBytes = 0;
		/* test download into memory */
		bestv::BtvVector<btvU8> destinationBuffer;
		//bestv::BtvString URL1("http://www.coolhunting.com/images/funky2.jpg"); 

		bestv::BtvString URL1("http://127.0.0.1:8080/koala.jpg");
		//bestv::BtvString URL1("http://127.0.0.1/koala.jpg");

		bestv::HttpFileDownload httpFileDownloadMem(URL1, 
			destinationBuffer, 
			BandwidthManager::HIGH_PRIORITY);

		if(httpFileDownloadMem.start() != BTV_NO_ERROR) {
			printf("Error in start()\n");
			printf("Press Return To Finish\n");
			return;
		}

		printf("Downloading %s -> memory\n", URL1.data());

		do{
			status = httpFileDownloadMem.downloadDataChunk();	
		}
		while(status == HttpFileDownload::DOWNLOAD_NOT_FINISHED);

		status = httpFileDownloadMem.getStatus();
		
		if(status != HttpFileDownload::DOWNLOAD_FINISHED){
			printf("Error in download. Code %d\n", (int)status);
			printf("Press Return To Finish\n");
			return ;
		}

		httpFileDownloadMem.stop();
		downloadedBytes = httpFileDownloadMem.getNumberOfDownloadedBytes();
		int k;
		k = destinationBuffer.size();
		
		printf("Test Ended. Bytes downloaded %d\n", downloadedBytes.getL());
		printf("destinationBuffer size %d\n", k);
		
		BtvString filenameTest2("httpFileDownloadTest2.jpg");
				
		printf("Writing Memory Buffer To File -> %s\n", filenameTest2.data());
		
		FILE *destFile;
		destFile = fopen(filenameTest2.data(), "wb");
		if(!destFile){
			printf("Could not open %s for writing\n", filenameTest2.data());
			
		}else{
			btvU8 byte;
			size_t ret;
			for(unsigned i = 0; i < destinationBuffer.size(); i++){
				byte = destinationBuffer[i];
				ret = fwrite(&byte, 1, 1, destFile);
				if(ret != 1){
					printf("Error writing to file\n");
					break;
				}
			}
			fclose(destFile);
		}
		printf("COMPARE THE ORIGIN FILE AND THE DOWNLOADED FILE BEFORE CONTINUING WITH THE NEXT TEST\n");
		printf("Press Return To Continue\n");
		getchar();
			
	}

	
	if(0){
		printf("Test Resume Download\n");
		downloadedBytes = 0;
		#define FIRST_PART_SIZE 3000000

		// test resume download 
		//URL = "http://www.booooooom.com/wp-content/uploads/2009/03/andy_j_miller_01.jpg"; 
		URL = "http://127.0.0.1:8080/9MB.ts";
		
		fileName = "resumeDownloadTest.ts";
		
		bestv::BtvUInt64 startIdxxx(0,0);
		
		bestv::HttpFileDownload httpFileDownloadd(
				URL, 
				fileName, 
				BandwidthManager::HIGH_PRIORITY, 
				startIdxxx);
				
		if(httpFileDownloadd.start() != BTV_NO_ERROR){
			printf("Error in start()\n");
			printf("Press Return To Finish\n");
			getchar();
			return ;
		}
		
		printf("Downloading %s -> %s\n", URL.data(), fileName.data());
		
		do{
			status = httpFileDownloadd.downloadDataChunk();	
			downloadedBytes = httpFileDownloadd.getNumberOfDownloadedBytes();
			if(downloadedBytes >= FIRST_PART_SIZE){
				break;
			}
		}while(status == HttpFileDownload::DOWNLOAD_NOT_FINISHED);

		httpFileDownloadd.stop();

		downloadedBytes = httpFileDownloadd.getNumberOfDownloadedBytes();

		printf("Finished downloading the first part\n");
		printf("Donwloaded %d bytes\n", downloadedBytes.getL());
		printf("Press Return to download the second part\n");
		getchar();

		
		bestv::BtvUInt64 startIdx1 = downloadedBytes;
		downloadedBytes = 0;
		
		bestv::HttpFileDownload httpFileDownloadd1(
				URL, 
				fileName, 
				BandwidthManager::HIGH_PRIORITY, 
				startIdx1);
				
		if(httpFileDownloadd1.start() != BTV_NO_ERROR){
			printf("Error in start()\n");
			printf("Press Return To Finish\n");
			getchar();
			return ;
		}				
		
		printf("Downloading (2nd part) %s -> %s\n", URL.data(), fileName.data());
				
		do{
			status = httpFileDownloadd1.downloadDataChunk();	
		}
		while(status == HttpFileDownload::DOWNLOAD_NOT_FINISHED);

		downloadedBytes = httpFileDownloadd1.getNumberOfDownloadedBytes();

		status = httpFileDownloadd1.getStatus();
		
		if(status != HttpFileDownload::DOWNLOAD_FINISHED){
			printf("Error in download. Code %d\n", (int)status);
			printf("Press Return To Finish\n");
			return ;
		}
		
		printf("Finished downloading the second part\n");
		printf("Downloaded %d bytes\n", downloadedBytes.getL());
		printf("COMPARE THE ORIGIN FILE AND THE DOWNLOADED FILE BEFORE CONTINUING WITH THE NEXT TEST\n");
		printf("Press Return To Finish\n");
		getchar();
	}
	
	if(0){
		/*
			Test resume download when the whole file was already there!
			It detects a range problem and downloads the file from scratch again.
		*/
		printf("Test Resume Download\n");
		downloadedBytes = 0;
		
		// test resume download 
		//URL = "http://www.booooooom.com/wp-content/uploads/2009/03/andy_j_miller_01.jpg"; 
		URL = "http://127.0.0.1:8080/koala.jpg";
		
		fileName = "resumeDownloadTest.ts";
		
		bestv::BtvUInt64 startIdxxx(0,0);
		
		bestv::HttpFileDownload httpFileDownloadd(
				URL, 
				fileName, 
				BandwidthManager::HIGH_PRIORITY, 
				startIdxxx);
				
		if(httpFileDownloadd.start() != BTV_NO_ERROR){
			printf("Error in start()\n");
			printf("Press Return To Finish\n");
			getchar();
			return ;
		}
		
		printf("Downloading %s -> %s\n", URL.data(), fileName.data());
		
		do{
			status = httpFileDownloadd.downloadDataChunk();	
		}while(status == HttpFileDownload::DOWNLOAD_NOT_FINISHED);

		httpFileDownloadd.stop();

		downloadedBytes = httpFileDownloadd.getNumberOfDownloadedBytes();

		printf("Finished downloading the whole file\n");
		printf("Donwloaded %d bytes\n", downloadedBytes.getL());
		printf("Press Return to download the second part starting at %d bytes\n", 
			downloadedBytes.getL());
		getchar();

		
		bestv::BtvUInt64 startIdx1 = downloadedBytes;
		downloadedBytes = 0;
		
		bestv::HttpFileDownload httpFileDownloadd1(
				URL, 
				fileName, 
				BandwidthManager::HIGH_PRIORITY, 
				startIdx1);
				
		if(httpFileDownloadd1.start() != BTV_NO_ERROR){
			printf("Error in start()\n");
			printf("Press Return To Finish\n");
			getchar();
			return ;
		}				
		
		printf("Downloading (2nd part) %s -> %s\n", URL.data(), fileName.data());
				
		do{
			status = httpFileDownloadd1.downloadDataChunk();	
		}
		while(status == HttpFileDownload::DOWNLOAD_NOT_FINISHED);

		downloadedBytes = httpFileDownloadd1.getNumberOfDownloadedBytes();

		status = httpFileDownloadd1.getStatus();
		
		if(status != HttpFileDownload::DOWNLOAD_FINISHED){
			printf("Error in download. Code %d\n", (int)status);
			printf("Press Return To Finish\n");
			return ;
		}
		
		printf("Finished downloading the second part\n");
		printf("Downloaded %d bytes\n", downloadedBytes.getL());
		printf("COMPARE THE ORIGIN FILE AND THE DOWNLOADED FILE BEFORE CONTINUING WITH THE NEXT TEST\n");
		printf("Press Return To Finish\n");
		getchar();
	}


	printf("End Of Main\n");
	printf("Press Return To Finish\n");
	getchar();
}